package com.wit.dao.impl;

import hibernate.HibernateSessionFactory;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;

import com.wit.dao.CompanyDao;
import com.wit.po.Company;

public class CompanyDaoImpl implements CompanyDao{

	
	public boolean addCompany(Company company) {
		Session s = null;
		Transaction tx = null;
		boolean isFinished = false;
		try{
			s = HibernateSessionFactory.getSession();
			tx =  s.beginTransaction();
			s.save(company);
			tx.commit();
			s.close();
			isFinished = true;
		}catch(HibernateException e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return isFinished;
	}

	
	public boolean deleteCompany(Company company) {
		Session s = null;
		Transaction tx = null;
		boolean isFinished = false;
		try{
			s = HibernateSessionFactory.getSession();
			tx =  s.beginTransaction();
			s.delete(company);
			tx.commit();
			s.close();
			isFinished = true;
		}catch(HibernateException e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return isFinished;
	}

	
	public List<Company> findCompanyByCondition(DetachedCriteria dc, Integer startRecord, Integer pageSize) {
		List<Company> compList = new ArrayList<Company>();
		try{
			compList = (List<Company>)dc.getExecutableCriteria(HibernateSessionFactory.getSession()).setFirstResult(startRecord).setMaxResults(pageSize).list();
		}catch(HibernateException e){
			e.printStackTrace();
		}
		return compList;
	}

	@SuppressWarnings("unchecked")
	
	public Company findCompanyByIdenfier(String companyNum) {
		Session s = null;
		Company company = null;
		try{
			s = HibernateSessionFactory.getSession();
			List<Company> compList = (List<Company>)s.createQuery("FROM Company WHERE companyNum = ?").setString(0, companyNum).list();
			if(compList.size() == 1)
				company = compList.get(0);
			s.close();
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return company;
	}

	
	public boolean modifyCompany(Company company) {
		Session s = null;
		Transaction tx = null;
		boolean isFinished = false;
		try{
			s = HibernateSessionFactory.getSession();
			tx =  s.beginTransaction();
			s.update(company);
			tx.commit();
			s.close();
			isFinished = true;
		}catch(HibernateException e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return isFinished;
	}

	
	public Integer findCompanyCount(DetachedCriteria dc) {
		List<Integer> countList = new ArrayList<Integer>();
		try{
			countList = (List<Integer>)dc.getExecutableCriteria(HibernateSessionFactory.getSession()).list();
		}catch(HibernateException e){
			e.printStackTrace();
		}
		return countList.get(0);
	}

}
